package com.light.buttons;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import com.heyzap.sdk.HeyzapLib;
import com.huw.debuger.FileIO;
import com.light.gamecomponents.IGameComponents;
import com.light.roller.BigMenuWorld1;
import com.light.roller.BigMenuWorld2;
import com.light.roller.BigMenuWorld3;
import com.light.roller.ResourceManager;

public class HeyzapButton implements IUpdateHandler, IGameComponents {

	protected TiledTextureRegion m_textureRegion;
	protected BaseGameActivity m_game;
	protected AnimatedSprite m_sprite;
	protected String m_resourceName = "HeyzapButton";
	
	public HeyzapButton(BaseGameActivity game)
	{
		m_game = game;
	}
	
	@Override
	public void onLoadEngine() { }

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources(m_resourceName, m_game);
		m_textureRegion = ResourceManager.getTiledTextureRegion(m_resourceName);
		
		
	}

	@Override
	public void unLoadResources() {
		
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWrold) {
		
		m_sprite = new AnimatedSprite(0, 0, 64, 64, m_textureRegion) 
		{
			@Override
        	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
        	{
				if (pSceneTouchEvent.isActionDown())
				{
					int tempUnlock = FileIO.LoadGame(m_game, FileIO.FileName.OpenNum);
					String forCheckin;
					if (tempUnlock <= 1)
					{
						forCheckin = "My journey begins!";
					}
					else if (tempUnlock <= 11)
					{
						String levelName = BigMenuWorld1.getStageNameArray()[tempUnlock - 2];
						String[] stringArray = levelName.split("\\W+");
						levelName = "";
						for (int i = 0; i < stringArray.length; i++)
						{
							if (stringArray[i] != "" && stringArray[i] != "\n")
							{
								if (i == 0)
									levelName = stringArray[i];
								else
									levelName = levelName + " " + stringArray[i];
							}
						}
						forCheckin = "I finished level \"" + levelName + "\"." + " Constant C will make you smarter!!" + "\nhttps://market.android.com/details?id=com.light.roller";
					}
					else if (tempUnlock <= 21)
					{
						String levelName = BigMenuWorld2.getStageNameArray()[tempUnlock - 12];
						String[] stringArray = levelName.split("\\W+");
						levelName = "";
						for (int i = 0; i < stringArray.length; i++)
						{
							if (stringArray[i] != "" && stringArray[i] != "\n")
							{
								if (i == 0)
									levelName = stringArray[i];
								else
									levelName = levelName + " " + stringArray[i];
							}
						}
						forCheckin = "I finished level \"" + levelName + "\"." + " Constant C will make you smarter!!" + "\nhttps://market.android.com/details?id=com.light.roller";
					}
					else if (tempUnlock <= 29)
					{
						String levelName = BigMenuWorld3.getStageNameArray()[tempUnlock - 22];
						String[] stringArray = levelName.split("\\W+");
						levelName = "";
						for (int i = 0; i < stringArray.length; i++)
						{
							if (stringArray[i] != "" && stringArray[i] != "\n")
							{
								if (i == 0)
									levelName = stringArray[i];
								else
									levelName = levelName + " " + stringArray[i];
							}
						}
						forCheckin = "I finished level \"" + levelName + "\"." + " Constant C will make you smarter!!" + "\nhttps://market.android.com/details?id=com.light.roller";
					}
					else
						forCheckin = "I'm handsome";
					HeyzapLib.checkin(m_game, forCheckin);
					return false;
				}
				return true;
        	}
		};
		scene.attachChild(m_sprite);
		scene.registerTouchArea(m_sprite);
	}

	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}
	
	public void setPosition (int x, int y)
	{
		m_sprite.setPosition(x, y);
	}

}
